﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;
using DAL.Persistencia;

namespace DAL.Persistencia
{
    public class QuoteTransDAL
    {

        private NewTierraEntities Con;

        public QuoteTransDAL()
        {
            Con = new NewTierraEntities();
        }

        public void Salvar(Quote_Transf q)
        {
            try
            {
                Con.AddToQuote_Transf(q);
                Con.SaveChanges();
            }
            catch 
            {                
                throw;
            }
        }

        public Quote_Transf ObterPorId(int IdQt)
        {
            try
            {
                return Con.Quote_Transf.Where(q => q.Quote_Transf_id == IdQt).SingleOrDefault();
            }
            catch 
            {                
                throw;
            }
        }

        public List<Quote_Transf> ObterPorIdTC(int IdRange)
        {
            try
            {
                return Con.Quote_Transf.Where(q => q.Range_id == IdRange).ToList();
            }
            catch
            {
                throw;
            }
        }        

        public void ExcluirPorFile(int idFile)
        {
            try
            {
                var query = Con.Quote_Transf.Where(q => q.File_id == idFile).ToList();

                foreach (Quote_Transf item in query)
                {
                    Con.DeleteObject(item);
                    Con.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }

        public void ExcluirLista(List<Quote_Transf> lista)
        {
            try
            {
                foreach (Quote_Transf item in lista)
                {
                    Con.DeleteObject(item);
                    Con.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarNew(Quote_Transf novo)
        {
            try
            {

                Quote_Transf ftrnew = ObterPorId(novo.Quote_Transf_id);

                ftrnew.S_merc_tarif_vendaNet = novo.S_merc_tarif_vendaNet;
                ftrnew.Transf_vendaNet = novo.Transf_vendaNet;
                ftrnew.Transf_venda = novo.Transf_venda;
                ftrnew.Transf_total = novo.Transf_total;
                ftrnew.Transf_valor = novo.Transf_valor;
                ftrnew.markup = novo.markup;
                ftrnew.markupNet = novo.markupNet;
                ftrnew.desconto = novo.desconto;
                ftrnew.descontoNet = novo.descontoNet;
                ftrnew.Qtd_Transf = novo.Qtd_Transf;
                ftrnew.Paxs = novo.Paxs;
                ftrnew.Paying_Pax = novo.Paying_Pax;
                ftrnew.Supp_Paying_Pax = novo.Supp_Paying_Pax;

                Con.SaveChanges();

            }
            catch 
            {                
                throw;
            }
        }

        public void Excluir(Quote_Transf q)
        {
            try
            {
                Con.DeleteObject(q);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public Quote_Transf ObterTC(int IdFile, int take)
        {
            try
            {
                take = take - 1;

                return Con.Quote_Transf.Where(q => q.File_id == IdFile &&
                                                    q.TC == true)
                                                    .OrderBy(q => q.Data_From)
                                                    .ThenBy(q => q.Data_To)
                                                    .ThenBy(q => q.Quote_Transf_id)
                                                    .Skip(take)
                                                    .Take(1).SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

        public List<Quote_Transf> ListarTodos(int IdFile)
        {
            try
            {
                return Con.Quote_Transf.Where(f => f.File_id == IdFile).ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<Quote_Transf> ListarTodos()
        {
            try
            {
                return Con.Quote_Transf.ToList();
            }
            catch
            {
                throw;
            }
        }

        public List<Quote_Transf> ListarTodos(DateTime dataInicio, DateTime dataFim)
        {
            try
            {
                return Con.Quote_Transf.Where(f => f.Data_From >= dataInicio && f.Data_From < dataFim).ToList();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarValores(Quote_Transf novo)
        {
            try
            {
                Quote_Transf antigo = ObterPorId(novo.Quote_Transf_id);

                antigo.S_merc_tarif_vendaNet = novo.S_merc_tarif_vendaNet;
                antigo.Transf_vendaNet = novo.Transf_vendaNet;
                antigo.Transf_venda = novo.Transf_venda;
                antigo.Transf_total = novo.Transf_total;
                antigo.Transf_valor = novo.Transf_valor;
                antigo.markup = novo.markup;
                antigo.markupNet = novo.markupNet;
                antigo.desconto = novo.desconto;
                antigo.descontoNet = novo.descontoNet;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarStatus(Quote_Transf novo)
        {
            try
            {
                Quote_Transf antigo = ObterPorId(novo.Quote_Transf_id);

                antigo.Status = novo.Status;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void AtualizarFlight(Quote_Transf novo)
        {
            try
            {
                Quote_Transf antigo = ObterPorId(novo.Quote_Transf_id);

                antigo.Flights_id = novo.Flights_id;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

    }
}
